//
//  SunImgShows.m
//  Xspcf
//
//  Created by sunshine on 16/10/25.
//  Copyright © 2016年 小小算盘. All rights reserved.
//

#import "SunImgShows.h"
#define HEIGHT SCREENHEIGHT - 64
#define WIDTH  SCREENWIDTH
@interface SunImgShows ()<UIScrollViewDelegate>
{
    UIScrollView *_scroll;
}
@end

@implementation SunImgShows

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    self.title = [NSString stringWithFormat:@"1/%ld",(unsigned long)self.imgs.count];
    [self creatUI];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [_scroll setContentOffset:CGPointMake(SCREENWIDTH * self.currentIndex, 0)];
    self.title = [NSString stringWithFormat:@"%d/%d",_currentIndex + 1,(int)self.imgs.count];
}

- (void)creatUI{
    _scroll = [[UIScrollView alloc]init];
    [self.view addSubview:_scroll];
    _scroll.delegate = self;
    _scroll.showsHorizontalScrollIndicator = NO;
    [_scroll lt_All:SunEdgesMake(0, 0, 0, 0) save:YES];
    _scroll.contentSize = CGSizeMake(WIDTH * self.imgs.count, HEIGHT);
    _scroll.pagingEnabled = YES;
    for (int i = 0; i < self.imgs.count; i ++) {
        UIScrollView *scV = [[UIScrollView alloc]initWithFrame:CGRectMake(WIDTH * i, 0, WIDTH, HEIGHT)];
        [_scroll addSubview:scV];
        scV.delegate = self;
        scV.showsVerticalScrollIndicator = NO;
        scV.showsHorizontalScrollIndicator = NO;
        scV.maximumZoomScale = 2.0;
        scV.minimumZoomScale = 1.0;
        scV.contentSize = CGSizeMake(WIDTH , HEIGHT);
        UIImageView *img = [[UIImageView alloc]init];
        [scV addSubview:img];
        [img lt_XYcenterTo:nil save:YES];
        NSArray *H = [NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:|-[view(<=%f)]-|",WIDTH] options:0 metrics:nil views:@{@"view":img}];
        [scV addConstraints:H];
        NSArray *V = [NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:|-[view(<=%f)]-|",HEIGHT] options:0 metrics:nil views:@{@"view":img}];
        [scV addConstraints:V];
        [img sd_setImageWithURL:[NSURL URLWithString:_imgs[i]]];
        img.contentMode = UIViewContentModeScaleAspectFit;
        img.tag = i + 1;
        UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
        [doubleTap setNumberOfTapsRequired:2];
        [scV addGestureRecognizer:doubleTap];
    }
}

- (void)handleDoubleTap:(UITapGestureRecognizer *)tap{
    UIScrollView *scV = (UIScrollView *)tap.view;
    if (scV.zoomScale > 1.0){
        [scV setZoomScale:1.0 animated:YES];
    }else {
        [scV setZoomScale:2.0 animated:YES];
    }
}

-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale{
    if (scale == 1) {
        _scroll.scrollEnabled = YES;
    }else {
        _scroll.scrollEnabled = NO;
    }
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (scrollView == _scroll) {
        _currentIndex = scrollView.contentOffset.x / WIDTH;
        self.title = [NSString stringWithFormat:@"%d/%ld",_currentIndex + 1,(unsigned long)self.imgs.count];
    }
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    UIImageView *img = [_scroll viewWithTag:_currentIndex + 1];
    return img;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
